//
//  KDWeibo.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/26.
//  Copyright © 2015年 魏强. All rights reserved.
//  微博模型

#import "KDWeibo.h"
#import "MJExtension.h"
#import "KDPhoto.h"
#import "NSDate+Extension.h"
@implementation KDWeibo
//- (NSDictionary *)objectClassInArray
//{
//    return @{@"pic_urls" : [KDPhoto class]};
//}
+ (NSDictionary *)replacedKeyFromPropertyName{
    return @{@"ID":@"id"};
}
- (void)setPic_ids:(NSArray *)pic_ids{
    _pic_ids = [self objectArrayWithArray:pic_ids andThumbnail_pic:self.thumbnail_pic andBmiddle_pic:self.bmiddle_pic];
}
//- (NSArray *)pic_ids{
//    
//    _pic_ids = [self objectArrayWithArray:_pic_ids andThumbnail_pic:self.thumbnail_pic andBmiddle_pic:self.bmiddle_pic];
//    return _pic_ids;
//}


- (NSArray *)objectArrayWithArray:(NSArray *)pics andThumbnail_pic:(NSString *)thumbnail_pic andBmiddle_pic:(NSString *)bmiddle_pic{
    NSString *thumbnail_head;
    NSString *bmiddle_head;
    for (NSInteger i = thumbnail_pic.length -1; i >= 0; i--) {
        
        unichar c =  [thumbnail_pic characterAtIndex:i];
        if (c =='/' ) {
            thumbnail_head = [thumbnail_pic substringToIndex:i+1];
            break;
        }
    }
    for (NSInteger i = bmiddle_pic.length -1; i >= 0; i--) {
        
        unichar c =  [bmiddle_pic characterAtIndex:i];
        if (c =='/' ) {
            bmiddle_head = [bmiddle_pic substringToIndex:i+1];
            break;
        }
    }
    NSMutableArray *arr = [NSMutableArray array];
    NSInteger i = 0;
    for (NSString *pic_id in pics) {
        KDPhoto *photo = [[KDPhoto alloc]init];
        photo.thumbnail_pic =[NSString stringWithFormat:@"%@%@.%@",thumbnail_head,pic_id,[thumbnail_pic pathExtension]];
        photo.bmiddle_pic = [NSString stringWithFormat:@"%@%@.%@", bmiddle_head,pic_id,[thumbnail_pic pathExtension]];
//        KDLog(@"%@\n%@",photo.thumbnail_pic, photo.bmiddle_pic);
        [arr addObject:photo];
        i++;
    }
    return arr;
}
- (NSString *)created_at
{
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    NSDateFormatter *dtfmt = [[NSDateFormatter alloc]init];
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
    fmt.dateFormat = @"yyyy-MM-dd";
    dtfmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    //标出这种时间表达形式的产地
    dtfmt.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    // 设置日期格式（声明字符串里面每个数字和单词的含义）
    // E:星期几
    // M:月份
    // d:几号(这个月的第几天)
    // H:24小时制的小时
    // m:分钟
    // s:秒
    // y:年
    //    _created_at = @"Tue Sep 30 17:06:25 +0600 2014";
    
    // 微博的创建日期
    NSDate *createDate = [[NSDate alloc] initWithTimeIntervalSince1970:_created_at.integerValue/1000.0];
    _date = [fmt stringFromDate:createDate];
//    NSDate *createDate = [fmt dateFromString:_created_at];
    // 当前时间
    NSDate *now = [NSDate date];
    
//     日历对象（方便比较两个日期之间的差距）
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // NSCalendarUnit枚举代表想获得哪些差值
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 计算两个日期之间的差值
    NSDateComponents *cmps = [calendar components:unit fromDate:createDate toDate:now options:0];
    NSString *date ;
    if ([createDate isThisYear]) { // 今年
        if ([createDate isYesterday]) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm";
            date = [fmt stringFromDate:createDate];
        } else if ([createDate isToday]) { // 今天
            if (cmps.hour >= 1) {
                date = [NSString stringWithFormat:@"%ld小时前", (long)cmps.hour];
            } else if (cmps.minute >= 1) {
                date = [NSString stringWithFormat:@"%ld分钟前", (long)cmps.minute];
            } else {
                date = @"刚刚";
            }
        } else { // 今年的其他日子
            fmt.dateFormat = @"MM-dd HH:mm";
            date =[fmt stringFromDate:createDate];
        }
    } else { // 非今年
        fmt.dateFormat = @"yyyy-MM-dd HH:mm";
        date = [fmt stringFromDate:createDate];
    }
    
    return date;
}

- (void)setSource:(NSString *)source
{
    // 正则表达式 NSRegularExpression
    // 截串 NSString
    if (!source.length) {//如果没有来源
        _source = source;
        return;
    }
    NSArray *strArr = [source componentsSeparatedByString:@">"];
//    NSRange range;
//    
//    range.location = [source rangeOfString:@">"].location + 1;
//    range.length = [source rangeOfString:@"</"].location - range.location;
//        range.length = [source rangeOfString:@"<" options:NSBackwardsSearch];
    NSMutableString *str =[NSMutableString stringWithString:[NSString stringWithFormat:@"来自%@", strArr[1]]] ;
    [str replaceCharactersInRange:[str rangeOfString:@"</a"] withString:@""];
    _source = str;
}
@end
